<!-- 角色列表 -->
<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="3">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="roleList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item, i1) in scope.row.auth_menus" :key="item.id">
              <el-col
                :class="['bdbottom',i1 === 0 && '.bdtop']"
                v-if="item.level === '0'"
                :span="24"
              >
                <el-tag type="primary">{{item.permissionName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col v-if="item.level === '1'" :offset="5" :span="5">
                <el-tag type="success">{{item.permissionName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col v-if="item.level === '2'" :offset="10" :span="14">
                <el-tag
                  type="warning"
                  closable
                  @close="removeAuthById(scope.row,item.id)"
                >{{item.permissionName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="授权人" prop="auth_name"></el-table-column>
        <el-table-column label="授权时间" prop="auth_time">
          <template slot-scope="scope">
            {{scope.row.auth_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="290px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="setAuth(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限树形对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setAuthdialogVisible"
      @close="treeDialogClose"
      width="50%"
    >
      <el-tree
        :data="treeData"
        :props="treeProps"
        show-checkbox
        node-key="path"
        ref="treeRef"
        :default-expand-all="true"
        :default-checked-keys="authList"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setAuthdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRoleAuth">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import menuConfig from '@/assets/menuConfig'
import {reqRole, reqAddPermission} from '@/api'
export default {
  data() {
    return {
      roleList: [], // 所有角色列表，包含权限
      authList: [], // 当前选择行的权限列表
      roleId: '', // 角色id
      commitRole: {}, // 更改权限后提交后台的角色数据
      setAuthdialogVisible: false,
      treeData: menuConfig,
      treeProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },

  created() {
    this.getRoleList()
  },

  methods: {
    async getRoleList() {
      const result = await reqRole()
      if (result.code !== 200) {
        this.$message.error('获取角色列表失败！')
      }
      this.roleList = result.data
      // this.$message.success('获取角色列表成功！')
    },

    // 根据id删除权限
    removeAuthById(role, authId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 不建议这样做，会刷新页面，应该直接改变role的数据
          // this.getRoleList()
          role.auth_menus[1].permissionName = '已删除'
          role.auth_menus[6].permissionName = '已删除'
          role.auth_menus[10].permissionName = '已删除'
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    // 分配权限
    setAuth(role) {
      this.authList = role.menus
      this.roleId = role.id
      this.setAuthdialogVisible = true
    },

    // 权限模态框确定提交
    async changeRoleAuth() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      this.commitRole.menus = keys
      this.commitRole.id = this.roleId
      this.commitRole.auth_name = 'admin'
      this.commitRole.auth_time = Date.now()

      const result = await reqAddPermission(this.commitRole)
      if (result.code !== 200) {
        this.$message.error('更新权限失败！')
      }
      this.$message.success('更新权限成功！')
      this.getRoleList()
      this.setAuthdialogVisible = false
    },

    // 树形结构模态框关闭
    treeDialogClose() {
      // 清空树形结构选中的数据
      this.authList = []
      this.$refs.treeRef.setCheckedKeys([])
    }
  }
}
</script>
<style lang='less' scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
</style>
